package collection;

import java.util.ArrayList;
import java.util.List;

/**
 * List实现类
 */
public class ListExercise02 {

    public static void main(String[] args) {

        /*
            用List的实现类添加三本图书 并遍历 打印如下效果
            名称: xx      价格: xx      作者: xx
            名称: xx      价格: xx      作者: xx
            名称: xx      价格: xx      作者: xx

            要求：
            1)按价格排序，从低到高(冒泡法)
            2)要求使用ArrayList LinkedList Vector 三种集合实现
         */

        List list = new ArrayList();
        list.add(new Books("张三", 53.0, "aaa"));
        list.add(new Books("李四", 43.0, "bbb"));
        list.add(new Books("王五", 33.0, "ccc"));

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.size() - i - 1; j++) {
                Books book1 = (Books)(list.get(j));
                Books book2 = (Books)(list.get(j + 1));
                if (book1.getPrice() > book2.getPrice()) {
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }

        System.out.println("====冒泡排序后集合如下====");
        System.out.println("list=" + list);
    }
}

class Books {

    private String name;

    private double price;

    private String author;

    public Books(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Books{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}

